package com.xyzwps.paimon.furina.modules.auth.entity

import com.xyzwps.paimon.furina.common.value.Phone
import kotlinx.datetime.LocalDateTime
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.kotlin.datetime.datetime

class UserAccount(
    val id: Long,
    val phone: Phone,
    val locked: Boolean,
    val disabled: Boolean,
    val createTime: LocalDateTime,
    val updateTime: LocalDateTime
) {

    constructor(row: ResultRow) : this(
        id = row[TABLE.id].value,
        phone = Phone(row[TABLE.phone]),
        locked = row[TABLE.locked],
        disabled = row[TABLE.disabled],
        createTime = row[TABLE.createTime],
        updateTime = row[TABLE.updateTime],
    )

    companion object {
        object TABLE : LongIdTable("user_account", columnName = "user_id") {
            val phone: Column<String> = varchar("phone", 16)
            val locked: Column<Boolean> = bool("locked").default(false)
            val disabled: Column<Boolean> = bool("disabled").default(false)
            val createTime: Column<LocalDateTime> = datetime("create_time")
            val updateTime: Column<LocalDateTime> = datetime("update_time")

            init {
                uniqueIndex(customIndexName = "uk_user_account_phone", phone)
            }
        }
    }
}